﻿/*
 * Created by SharpDevelop.
 * User: fit_admin
 * Date: 11.05.2010
 * Time: 16:30
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Text.RegularExpressions;

namespace eProject.View.Validation
{
    /// <summary>
    /// Description of ValidationRoutines.
    /// </summary>
    public static class ValidationRoutines
    {		
        public static readonly Predicate<string> ValidateEmployeeNamePredicate = ValidateEmployeeName;
		
        public static readonly Predicate<string> ValidateEMailPredicate = ValidateEMail;
		
        public static readonly Predicate<string> ValidatePhoneNumberPredicate = ValidatePhoneNumber;

        public static readonly Predicate<string> ValidateTaskNamePredicate = ValidateTaskName;

        public static readonly Predicate<string> ValidateDescriptionPredicate = ValidateDescription;

        public static readonly Predicate<string> ValidateTaskDurationPredicate = ValidateTaskDuration;

        public static readonly Predicate<string> ValidateDayLengthPredicate = ValidateDayLength;

        public static bool ValidateEmployeeName(string name)
        {
            if (String.IsNullOrEmpty(name)) return false;

            return EmployeeNameRegex.IsMatch(name);
        }
		
        public static bool ValidatePhoneNumber(string phone)
        {
            return phone != null && (phone == "" || PhoneNumberRegex.IsMatch(phone));
        }

        public static bool ValidateEMail(string email)
        {
            return email != null && EMailRegex.IsMatch(email);
        }

        public static bool ValidateDescription(string descr)
        {
            return descr != null;
        }

        public static bool ValidateTaskName(string name)
        {
            return !String.IsNullOrEmpty(name);
        }

        public static bool ValidateTaskDuration(string duration)
        {
            long result;
            if (!long.TryParse(duration, out result)) return false;
            return result > 0;

        }

        public static bool ValidateDayLength(string length)
        {
            double res;
            if (!double.TryParse(length, out res)) return false;
            return res >= 0 && res <= 24;
        }

        private static readonly Regex EMailRegex = new Regex(@"^[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$");

        private static readonly Regex PhoneNumberRegex = new Regex(@"^\+?(?:\d\-?)*\d$");

        private static readonly Regex EmployeeNameRegex = new Regex(@"^[\w-_]+$");
    }
}